Array Methods In Js

Posted on February 5, 2025 by Vishesh Namdev
Python C C++ Javascript Java
Array Methods in JS

Array Methods
Array methods are built-in functions that allow you to perform operations on arrays in a concise and efficient way. They provide a powerful set of tools for manipulating and working with arrays without the need for manual loops.

Here are some of the most commonly used array methods in JavaScript:

1. map():

  • Use: Creates a new array by applying a given function to each element of the original array.
  • Example:
  •  const numbers = [1, 2, 3, 4, 5];
      const doubled = numbers.map(number => number * 2); 
      console.log(doubled); // Output: [2, 4, 6, 8, 10]

    2. filter():

  • Use: Creates a new array containing only the elements that satisfy a given condition.
  • Example:
  •   const ages = [18, 25, 16, 30, 20];
        const adults = ages.filter(age => age >= 18);
        console.log(adults); // Output: [18, 25, 30, 20] 

    3. forEach():

  • Use: Executes a provided function once for each element in the array.
  • Example:
  •   const names = ["Alice", "Bob", "Charlie"];
      names.forEach(name => console.log(name));

    4. sort():

  • Use: Sorts the elements of an array in place and returns the sorted array.
  • Example:
  •   const fruits = ["banana", "apple", "orange"];
      fruits.sort(); 
      console.log(fruits); // Output: ["apple", "banana", "orange"]

    5. join():

  • Use: Creates and returns a new string by concatenating all of the elements in an array, separated by a specified separator.
  • Example:
  •   const fruits = ["Apple", "Banana", "Cherry"];
      const result = fruits.join(" - ");
      console.log(result); // Output: "Apple - Banana - Cherry"

    6. slice():

  • Use: Returns a shallow copy of a portion of an array into a new array selected from start to end (end not included).
  • Example:
  •   const numbers = [1, 2, 3, 4, 5];
      const sliced = numbers.slice(1, 3); 
      console.log(sliced); // Output: [2, 3]

    7. concat():

  • Use: Creates a new array by concatenating the specified arrays or values to the end of the current array.
  • Example:
  •   const array1 = [1, 2, 3];
        const array2 = [4, 5, 6];
        const combined = array1.concat(array2); 
        console.log(combined); // Output: [1, 2, 3, 4, 5, 6]

    8. includes():

  • Use: Determines whether an array includes a certain value among its entries, returning true or false as appropriate.
  • Example:
  •   const numbers = [1, 2, 3, 4, 5];
      const includesThree = numbers.includes(3); 
      console.log(includesThree); // Output: true

    9. push():

  • Use: Adds one or more elements to the end of an array and returns the new length of the array.
  • Example:
  •   const numbers = [1, 2, 3];
      numbers.push(4, 5); 
      console.log(numbers); // Output: [1, 2, 3, 4, 5]

    10. pop():

  • Use: Removes the last element from an array and returns that element.
  • Example:
  •   const numbers = [1, 2, 3];
      const last = numbers.pop(); 
      console.log(numbers); // Output: [1, 2]
      console.log(last); // Output: 3
    ๐Ÿ“ขImportant Note๐Ÿ“ข

    How did you feel about this post?

    ๐Ÿ˜ ๐Ÿ™‚ ๐Ÿ˜ ๐Ÿ˜• ๐Ÿ˜ก

    Was this helpful?

    ๐Ÿ‘ ๐Ÿ‘Ž